package com.dove.config;

import com.dove.task.DefAsyncRedisNoticeTask;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@EnableCaching
public class RedisCacheConfig {
	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);

		// 测试
		container.addMessageListener(listenerAdapter, new PatternTopic("redis.channel.test"));
		return container;
	}

	@Bean
	MessageListenerAdapter listenerAdapter(DefAsyncRedisNoticeTask notice) {
		return new MessageListenerAdapter(notice, "onMessage");
	}

}